# -*- coding: utf-8 -*-
"""
Created on Thu May  4 09:25:53 2017

@author: qiang.qian
"""

import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as local 

from plotly.graph_objs import Scatter, Layout,XAxis,YAxis

import datetime
today = datetime.date.today().strftime("%Y%m%d")

df = pd.read_table('b:\\may_data_active_' + today + '.txt',index_col='active_date')

be = df.ix[:'2017-05-02']['active_count']
be = be[(be < 5000) & (be > 1000)]
mean = be.mean()

df['mean'] = mean
df['fair'] = mean + 500;
df['ideal'] = mean + 1000;

df['before'] = df['active_count']
  
df.loc[:'2017-05-02','active_count']= np.NAN  
df.loc['2017-05-04':,'before']= np.NAN  
  
x=df.index.values.tolist()
x_rev=x[::-1]  
  

event = go.Scatter(
    x=x,
    y=df['active_count'],
    name='活动中'
)

target = go.Scatter(
    x=x+x_rev,
    y=df['fair'].tolist()+df['ideal'].tolist(),
    fill='tozerox',
    fillcolor='rgba(231,107,243,0.2)',
    line=go.Line(color='transparent'),
    name='活动目标',
)

before_mean = go.Scatter(
    x=x,
    y=df['mean'],
    name='活动前平均'
)

before = go.Scatter(
    x=x,
    y=df['before'],
    name='活动前'
)

data = [before, before_mean,target,event]
layout = Layout(
    title='主动激活人数（目标：在原有基础上增加500-1000）',
    paper_bgcolor='rgb(255,255,255)',
    plot_bgcolor='rgb(229,229,229)',
    xaxis=XAxis(
        gridcolor='rgb(255,255,255)',
        showgrid=True,
        showline=False,
        showticklabels=True,
        tickcolor='rgb(127,127,127)',
        ticks='outside',
        zeroline=False
    ),
    yaxis=YAxis(
        gridcolor='rgb(255,255,255)',
        showgrid=True,
        showline=False,
        showticklabels=True,
        tickcolor='rgb(127,127,127)',
        ticks='outside',
        zeroline=False
    ),
)
fig = go.Figure(data=data, layout=layout)
plot_url = local.plot(fig, filename='may_event_active.html')
plot_url = py.iplot(fig, filename='may_event_active')

